package com.upay.android.client.activity;

import static com.upay.android.client.helper.Keys.GROUPID;
import static com.upay.android.client.helper.Keys.TOKEN;
import static com.upay.android.client.helper.Keys.UPAY_PREFERENCES;
import static com.upay.android.client.helper.Keys.USERID;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.type.TypeReference;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.upay.android.client.R;
import com.upay.android.client.handler.CallbackHandler;
import com.upay.android.client.handler.GroupExactUsersCallbackHandler;
import com.upay.android.core.service.RestService;
import com.upay.android.domain.rest.RestRequest;
import com.upay.android.util.rest.RequestMethod;
import com.upay.android.util.rest.UrlHelper;
import com.upay.domain.account.ExactPaymentUser;
import com.upay.domain.response.ResponseEnvelope;

public class GroupExactUsersActivity extends AbstractUPayActivity {

	/* ---- Constants ---- */
	public static final int MENU_ADD_USER = 0; 

	/* ---- Instance Variables ---- */
    private Long groupId = null;
	private final CallbackHandler<List<ExactPaymentUser>> groupExactUsersCallbackHandler = new GroupExactUsersCallbackHandler(this);

	
	/* ---- Constructors ---- */
	
	/* ---- Business Methods ---- */
	

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.groupId = getIntent().getExtras().getLong(GROUPID);
        
        SharedPreferences sharedPreferences = getSharedPreferences(UPAY_PREFERENCES, 0);
   	    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
   	    params.add(new BasicNameValuePair(USERID, sharedPreferences.getString(USERID, null)));
   	    params.add(new BasicNameValuePair(TOKEN, sharedPreferences.getString(TOKEN, null)));
   	    params.add(new BasicNameValuePair(GROUPID, groupId.toString()));
   	    
   	    RestRequest restRequest = new RestRequest(RequestMethod.POST, UrlHelper.GROUP_USERS, params);
   	    TypeReference<ResponseEnvelope<List<ExactPaymentUser>>> typeReference = new TypeReference<ResponseEnvelope<List<ExactPaymentUser>>>() {};
   	     
   	    // Execute Request
   	    RestService<List<ExactPaymentUser>> restService = new RestService<List<ExactPaymentUser>>(restRequest, typeReference, groupExactUsersCallbackHandler);
   	    restService.execute();
   	    
   	    setContentView(R.layout.groupexactusers);
   	   
    }
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.exactusergroupmenu, menu);
        return super.onCreateOptionsMenu(menu);
    }
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getOrder()) {
    	    case MENU_ADD_USER:
    	    	Intent intent = new Intent(this, GroupAvailableUsersActivity.class);
    	    	intent.putExtra(GROUPID, this.groupId);
    	    	startActivity(intent);
    	    	return true;
    	}
    	return super.onOptionsItemSelected(item);

    }
}
